package com.cb.domain.model.system;

import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.security.GrantedAuthority;



/**
 * Model class for AppRole. Add not database mapped fileds in this class.
 */

@javax.persistence.Entity
@Table(name="app_role")
public class AppRole extends AppRoleTbl implements GrantedAuthority {

	protected Long roleId;
	protected String roleName;
	protected String roleDesc;
	protected Short status;
	protected java.util.Set<AppResource> resources = new java.util.HashSet();
	
	/**
	 * 公共权限角色
	 */
	public static final String ROLE_PUBLIC="ROLE_PUBLIC";
	/**
	 * 后台公共权限角色
	 */
	public static final String ROLE_ADMIN_PUBLIC="ROLE_ADMIN_PUBLIC";
//	/**
//	 * 后台超级管理员角色
//	 */
//	public static final String ROLE_ADMINISTRATOR="ROLE_ADMINISTRATOR";
	
	/**
	 * 个人会员角色
	 */
	public static final String ROLE_MEMBER="ROLE_MEMBER";
	public static final String ROLE_ADMIN = "ROLE_ADMIN";

	
	
	@Id     
    @GeneratedValue(generator="Id_Generator")
    @GenericGenerator(name="Id_Generator", strategy="com.cb.common.util.IdGenerator")
	@Column(name="roleId")   
	public Long getRoleId() {
		return roleId;
	}

	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}

	@Column(name = "roleName")
	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	@Column(name = "roleDesc")
	public String getRoleDesc() {
		return roleDesc;
	}

	public void setRoleDesc(String roleDesc) {
		this.roleDesc = roleDesc;
	}

	@Column(name = "status")
	public Short getStatus() {
		return status;
	}

	public void setStatus(Short status) {
		this.status = status;
	}

	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name="role_res",joinColumns={@JoinColumn(name="roleId")},inverseJoinColumns={@JoinColumn(name="resourceId")})
	public java.util.Set<AppResource> getResources() {
		return resources;
	}

	public void setResources(java.util.Set<AppResource> resources) {
		this.resources = resources;
	}

	@Transient
	public boolean isPermitAll() {
		for (AppResource resource : resources) {
			if (StringUtils.equals(AppResource.RESOURCE_ALL, resource.getResourceName()))
				return true;
		}
		return false;
	}

	@Transient
	public boolean hasResource(String name) {
		for (AppResource resource : resources) {
			if (StringUtils.equals(name, resource.getResourceName()))
				return true;
		}
		return false;
	}

	@Transient
	public boolean hasNoResource(String name) {
		for (AppResource resource : resources) {
			if (StringUtils.equals(name, resource.getResourceName()))
				return false;
		}
		return true;
	}
	
	@Transient
	public String getAuthority() {
		return roleName;
	}

	@Transient
	public int compareTo(Object o) {
		return 0;
	}
}

